import { SkillBase } from '@/assets/skills/SkillBase.js'
import { GameObject } from '@/assets/GameObject.js'

// 闪现特效粒子类
class FlashParticle {
    constructor(x, y, radius, lifespan, color, isVanish) {
        this.x = x
        this.y = y
        this.radius = radius
        this.maxRadius = radius
        this.lifespan = lifespan
        this.maxLifespan = lifespan
        this.color = color
        this.alpha = 1.0
        this.isVanish = isVanish // 是否是消失特效
    }

    update(timeDelta) {
        // 减少生命周期
        this.lifespan -= timeDelta

        // 计算透明度和缩放
        this.alpha = this.lifespan / this.maxLifespan

        // 根据是消失还是出现特效，调整粒子半径
        if (this.isVanish) {
            // 消失特效：粒子由小变大再消失
            this.radius = this.maxRadius * Math.sin(Math.PI * (1 - this.alpha)) * 1.5
        } else {
            // 出现特效：粒子由大变小
            this.radius = this.maxRadius * (1 - Math.sin(Math.PI * this.alpha * 0.5)) * 1.2
        }

        // 如果生命周期结束，返回false
        return this.lifespan > 0
    }

    draw(ctx) {
        ctx.beginPath()
        ctx.globalAlpha = this.alpha

        // 创建径向渐变
        const gradient = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius)

        // 设置渐变颜色
        if (this.color === 'gold') {
            gradient.addColorStop(0, 'rgba(255, 255, 200, ' + this.alpha + ')')
            gradient.addColorStop(0.4, 'rgba(255, 215, 0, ' + this.alpha * 0.8 + ')')
            gradient.addColorStop(1, 'rgba(218, 165, 32, 0)')
        } else if (this.color === 'yellow') {
            gradient.addColorStop(0, 'rgba(255, 255, 255, ' + this.alpha + ')')
            gradient.addColorStop(0.4, 'rgba(255, 255, 0, ' + this.alpha * 0.8 + ')')
            gradient.addColorStop(1, 'rgba(255, 215, 0, 0)')
        }

        ctx.fillStyle = gradient
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2)
        ctx.fill()

        // 添加发光效果
        ctx.shadowBlur = 15
        ctx.shadowColor = 'rgba(255, 215, 0, 0.8)'
        ctx.fill()
        ctx.shadowBlur = 0

        ctx.globalAlpha = 1.0
    }
}

// 闪现特效类
export class FlashEffect extends GameObject {
    constructor(gameMap, x, y, playerRadius, isVanish) {
        super()
        this.gameMap = gameMap
        this.x = x
        this.y = y
        this.playerRadius = playerRadius
        this.isVanish = isVanish

        this.particles = []
        this.createEffectParticles()
    }

    createEffectParticles() {
        // 特效颜色
        const colors = ['gold', 'yellow']

        // 在玩家位置周围生成粒子
        for (let i = 0; i < 20; i++) {
            // 随机角度和距离
            const angle = Math.random() * Math.PI * 2
            const distance = this.playerRadius * (0.2 + Math.random() * 1.2)

            // 计算粒子位置
            const px = this.x + Math.cos(angle) * distance
            const py = this.y + Math.sin(angle) * distance

            // 随机半径
            const radius = this.playerRadius * (0.1 + Math.random() * 0.4)

            // 随机生命周期
            const lifespan = 300 + Math.random() * 300

            // 随机颜色
            const color = colors[Math.floor(Math.random() * colors.length)]

            // 创建粒子
            this.particles.push(new FlashParticle(px, py, radius, lifespan, color, this.isVanish))
        }
    }

    update() {
        // 更新所有粒子
        for (let i = this.particles.length - 1; i >= 0; i--) {
            if (!this.particles[i].update(this.timeDelta)) {
                this.particles.splice(i, 1)
            }
        }

        // 绘制粒子
        this.draw()

        // 如果所有粒子都消失，销毁特效对象
        if (this.particles.length === 0) {
            this.destroy()
        }
    }

    draw() {
        // 绘制所有粒子
        for (const particle of this.particles) {
            particle.draw(this.gameMap.ctx)
        }
    }
}

export class Flash extends SkillBase {
    constructor(player) {
        super(player, 0) //15秒冷却
        this.player = player
        this.skillKey = 'F'
        this.skillName = 'flash'
        this.flashDistance = player.height * 0.3 //闪现距离上限

        // 加载闪现技能图标
        this.load_skill_icon('https://www.helloimg.com/i/2025/04/11/67f8a57d6ab75.png')
    }

    // 添加resize方法，更新闪现参数以匹配窗口大小变化
    resize() {
        // 更新闪现距离
        this.flashDistance = this.player.height * 0.3
    }

    // 计算两点之间的距离
    get_dist(x1, y1, x2, y2) {
        return Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    }

    cast(tx, ty) {
        if (this.player.isDead || this.is_on_cooldown()) {
            return false
        }

        // 记录起点位置
        const startX = this.player.info.x
        const startY = this.player.info.y

        // 计算闪现距离和角度
        let d = this.get_dist(this.player.info.x, this.player.info.y, tx, ty)
        d = Math.min(d, this.flashDistance) // 限制最大闪现距离

        let angle = Math.atan2(ty - this.player.info.y, tx - this.player.info.x)

        // 计算实际落点坐标
        const newX = this.player.info.x + d * Math.cos(angle)
        const newY = this.player.info.y + d * Math.sin(angle)

        // 创建消失特效
        new FlashEffect(this.gameMap, startX, startY, this.player.info.radius, true)

        // 更新玩家位置
        this.player.info.x = newX
        this.player.info.y = newY

        // 创建出现特效
        new FlashEffect(this.gameMap, newX, newY, this.player.info.radius, false)

        // 重置玩家移动状态
        this.player.moveLength = 0

        // 调用父类方法重置冷却
        super.cast()

        // 返回实际落点坐标和成功标志
        return {
            success: true,
            newX: newX,
            newY: newY,
        }
    }
}
